//
//  MineController.m
//  iMine
//
//  Created by gaoxiang on 09-3-11.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "MineController.h"
#import "ScoreClient.h"

@implementation MineController

- (id) init
{
	if (self != nil) {
		self = [super init];
		gameEngine = [GameEngine sharedGameEngine];
		privLocation = NSZeroPoint;
		dragging = NO;	}
	return self;
}

- (void)awakeFromNib
{		
	NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
	[notificationCenter addObserver:self selector:@selector(viewMouseUp:) name:@"MineViewMouseUp" object:nil];
	[notificationCenter addObserver:self selector:@selector(viewRightMouseDown:) name:@"MineViewRightMouseDown" object:nil];
	[notificationCenter addObserver:self selector:@selector(viewMouseDown:) name:@"MineViewMouseDown" object:nil];
	//[notificationCenter addObserver:self selector:@selector(viewMouseMoved:) name:@"MineViewMouseMoved" object:nil];
	[notificationCenter addObserver:self selector:@selector(viewMouseDragged:) name:@"MineViewMouseDragged" object:nil];
	[notificationCenter addObserver:self selector:@selector(viewOtherMouseUp:) name:@"MineViewOtherMouseUp" object:nil];
	[notificationCenter addObserver:self selector:@selector(viewOtherMouseDown:) name:@"MineViewOtherMouseDown" object:nil];
	[notificationCenter addObserver:self selector:@selector(viewOtherMouseDragged:) name:@"MineViewOtherMouseDragged" object:nil];
	
	[notificationCenter addObserver:self selector:@selector(newGameNotificationReceive:) name:@"newGameNotification" object:nil];
	
	[notificationCenter addObserver:self selector:@selector(remainCounterChanged:) name:@"remainCounterChanged" object:nil];
	[notificationCenter addObserver:self selector:@selector(secoundCounterChanged:) name:@"secoundCounterChanged" object:nil];
	
	[notificationCenter addObserver:self selector:@selector(gameStatusChanged:) name:@"gameStatusChanged" object:nil];
	
	[gameEngine newGame];
	
	[self renderView:YES];
}



- (IBAction)newGame:(id)sender {
		
    [gameEngine newGame];
}

- (IBAction)setBeginner:(id)sender
{
	[self setLevel:MINE_LEVEL1];
}

- (IBAction)setNovice:(id)sender
{
	[self setLevel:MINE_LEVEL2];
}

- (IBAction)setExpert:(id)sender
{
	[self setLevel:MINE_LEVEL3];
}

- (IBAction)testMineField:(id)sender
{
	[gameEngine testMineField];
	[self renderView:NO];
}


- (void)setLevel:(int)level {
	[gameEngine setLevel:level];
	[gameEngine newGame];
	[self renderView:YES];
}


- (void)viewMouseUp:(NSNotification *)notification
{
	NSEvent *theEvent = (NSEvent *)[notification object];
	
	NSPoint target = [theEvent locationInWindow];
	target.x -= 20;
	target.y -= 20;
	
	int command = MineCommandNormal;
	
	if (dragging || [theEvent modifierFlags] & NSAlternateKeyMask)
	{	
		[gameEngine hitTestPosition:privLocation withCommand:MineCommandFinishTip];
	}
	else if ([theEvent modifierFlags] & NSControlKeyMask)
	{
		[gameEngine hitTestPosition:privLocation withCommand:MineCommandSetFlag];
	}
	else
	{
		[gameEngine hitTestPosition:target withCommand:command];
	}
	
	dragging = NO;
	[self renderView:NO];
}
- (void)viewRightMouseDown:(NSNotification *)notification 
{
	NSEvent *theEvent = (NSEvent *)[notification object];
	if ([theEvent clickCount] > 1)
		return;
	
	NSPoint target = [theEvent locationInWindow];
	target.x -= 20;
	target.y -= 20;
	
	privLocation = target;
	
	int command = MineCommandSetFlag;
	
	[gameEngine hitTestPosition:target withCommand:command];
	
	[self renderView:NO];
}
- (void)viewMouseDown:(NSNotification *)notification 
{
	NSEvent *theEvent = (NSEvent *)[notification object];
	
	NSPoint target = [theEvent locationInWindow];
	target.x -= 20;
	target.y -= 20;
	
	privLocation = target;
	
	BOOL needRender = NO;
	
	int command = MineCommandNormal;
	if ([theEvent modifierFlags] & NSAlternateKeyMask)
	{	
		command = MineCommandTip;
		dragging = YES;
		[gameEngine hitTestPosition:target withCommand:command];
		needRender = YES;
	}

	if (needRender) [self renderView:NO];
}

- (void)viewMouseDragged:(NSNotification *)notification
{
	NSEvent *theEvent = (NSEvent *)[notification object];
	
	NSPoint target = [theEvent locationInWindow];
	target.x -= 20;
	target.y -= 20;

	if ([theEvent modifierFlags] & NSAlternateKeyMask)
	{	
		dragging = YES;
		[gameEngine hitTestPosition:privLocation withCommand:MineCommandCancelTip];
		privLocation = target;
		[gameEngine hitTestPosition:target withCommand:MineCommandTip];
	}
	
	[self renderView:NO];
}
- (void)viewOtherMouseUp:(NSNotification *)notification
{
	NSEvent *theEvent = (NSEvent *)[notification object];
	
	NSPoint target = [theEvent locationInWindow];
	target.x -= 20;
	target.y -= 20;
	
	[gameEngine hitTestPosition:privLocation withCommand:MineCommandFinishTip];
	
	[self renderView:NO];
}

- (void)viewOtherMouseDown:(NSNotification *)notification
{
	NSEvent *theEvent = (NSEvent *)[notification object];
	
	NSPoint target = [theEvent locationInWindow];
	target.x -= 20;
	target.y -= 20;
	
	privLocation = target;
	
	[gameEngine hitTestPosition:target withCommand:MineCommandTip];
	
	[self renderView:NO];
}

- (void)viewOtherMouseDragged:(NSNotification *)notification
{
	NSEvent *theEvent = (NSEvent *)[notification object];

	NSPoint target = [theEvent locationInWindow];
	target.x -= 20;
	target.y -= 20;
	
	[gameEngine hitTestPosition:privLocation withCommand:MineCommandCancelTip];
	privLocation = target;
	[gameEngine hitTestPosition:target withCommand:MineCommandTip];
	
	[self renderView:NO];
}

- (void)newGameNotificationReceive:(NSNotification *)notification
{
	[self renderView:YES];
}

- (void)secoundCounterChanged:(NSNotification *)notification
{
	[secondView setViewImage:[gameEngine secondConterImage]];
	[secondView setNeedsDisplay:YES];
}

- (void)remainCounterChanged:(NSNotification *)notification
{
	[remainView setViewImage:[gameEngine remainConterImage]];
	[remainView setNeedsDisplay:YES];
}

- (void)gameStatusChanged:(NSNotification *)notification
{
	NSNumber *status = (NSNumber *)[notification object];
	switch ([status intValue]) {
		case Win:
			[resetButton setImage:[NSImage imageNamed:@"win"]];
			break;
		case Lose:
			[resetButton setImage:[NSImage imageNamed:@"lose"]];
			break;
		case OK:
		default:
			[resetButton setImage:[NSImage imageNamed:@"ok"]];
			break;
	}
}

- (void)renderView:(BOOL)isResize
{
	[mineView setViewImage:[gameEngine mineFieldImage]];
	if (isResize)
		[mineView resizeView:[[gameEngine mineFieldImage] size]];
	[mineView setNeedsDisplay:YES];
}

- (void) dealloc
{
	NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
	[nc removeObserver:self];
	[super dealloc];
}


@end
